﻿using System;
using System.Web.Services;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class KhoiLuong : System.Web.UI.Page
    {
        #region Properties

        private HopDongBiz _hopDongBiz;
        private HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private static KhoiLuongBiz _khoiLuongBiz;

        private static KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        private int _hopDongId;

        private int HopDongId
        {
            get
            {
                if (_hopDongId <= 0)
                    int.TryParse(Request.QueryString["hopDongId"], out _hopDongId);
                return _hopDongId;
            }
        }

        private HopDongView _hopDongView;
        private HopDongView CurrentHopDongView
        {
            get
            {
                if (_hopDongView == null && HopDongId > 0)
                {
                    _hopDongView = HopDongManager.GetById(HopDongId);
                }
                return _hopDongView;
            }
        }

        private static PhieuXuatKhoBiz _phieuXuatKhoBiz;

        private static PhieuXuatKhoBiz PhieuXuatKhoManager
        {
            get { return _phieuXuatKhoBiz ?? (_phieuXuatKhoBiz = new PhieuXuatKhoBiz()); }
        }

        private static KhoiLuongVatTuBiz _khoiLuongVatTuBiz;
        private static KhoiLuongVatTuBiz KhoiLuongVatTuManager
        {
            get
            {
                return _khoiLuongVatTuBiz ?? (_khoiLuongVatTuBiz = new KhoiLuongVatTuBiz());
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!WebCommon.CurrrentEmployee.IsAdmin &&
                    WebCommon.CurrrentEmployee.Id != CurrentHopDongView.EmployeeId)
                {
                    Response.Redirect("~/NoPermission.aspx");
                    return;
                }

                LoadHopDongData();
            }
        }

        #endregion

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object KhoiLuongList(int hopDongId)
        {
            var khoiluongs = KhoiLuongManager.KhoiLuongList(hopDongId);
            return new { Result = "OK", Records = khoiluongs };
        }

        [WebMethod(EnableSession = true)]
        public static object KhoiLuongDelete(int Id)
        {
            KhoiLuongManager.DeleteKhoiLuong(Id);
            return new { Result = "OK" };
        }

        [WebMethod(EnableSession = true)]
        public static object SearchKhoiLuong(int jtStartIndex, int jtPageSize, string jtSorting,
            string tenDonVi, string soHopDong, int tinhTrangThiCongId, int tinhTrangVatTuId, string matram, int doiThiCongId, int trangThaiHopDongId)
        {
            int totalRecords;

            var khoiluongs = KhoiLuongManager.SearchKhoiLuong(jtStartIndex, jtPageSize, jtSorting,
                tenDonVi, soHopDong, tinhTrangThiCongId, tinhTrangVatTuId, matram, WebCommon.CurrrentEmployee,
                doiThiCongId, trangThaiHopDongId,
                out totalRecords);
            return new { Result = "OK", Records = khoiluongs, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object PhieuXuatList(int hopDongId)
        {
            var records = PhieuXuatKhoManager.PhieuXuatKhoList(hopDongId);
            return new { Result = "OK", Records = records };
        }

        [WebMethod(EnableSession = true)]
        public static object PhieuXuatDelete(int Id)
        {
            PhieuXuatKhoManager.DeletPhieuXuatKho(Id);
            return new { Result = "OK" };
        }

        [WebMethod(EnableSession = true)]
        public static object GetKhoiLuongVatTus(int hopDongId)
        {
            return new { Result = "OK", Records = KhoiLuongVatTuManager.GetKhoiLuongVatTuViews(hopDongId) };
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteKhoiLuongVatTu(int Id)
        {
            KhoiLuongVatTuManager.Delete(Id);

            return new { Result = "OK" };
        }

        #endregion

        #region Helpers

        private void LoadHopDongData()
        {
            var hopDong = HopDongManager.GetById(HopDongId);
            if (hopDong != null)
            {
                ltrSoHopDong.Text = hopDong.SoHopDong;
                ltrTenHopDong.Text = hopDong.TenHopDong;
                ltrGiaTriChuaVat.Text = string.Format("{0:N0}", hopDong.GiaTriHopDongChuaVAT);
                ltrGiaTriVat.Text = string.Format("{0:N0}", hopDong.VAT);
                ltrGiaTriNhanCong.Text = string.Format("{0:N0}", hopDong.GiaTriNhanCong);
                ltrGiaTriTong.Text = string.Format("{0:N0}", hopDong.TongGiaTriHopDong);
                ltrGiaTriVatTu.Text = string.Format("{0:N0}", hopDong.GiaTriVatTu);
                if (hopDong.ThiCongXong)
                    ltrThiCongXong.Text = "Thi công xong, chờ hoàn công";
            }
        }

        #endregion
    }
}